//
//  MainViewController.m
//  FitTime
//
//  Created by dllo on 16/3/31.
//  Copyright © 2016年 赵建平. All rights reserved.
//

#import "MainViewController.h"
#import "VideoViewController.h"
#import "SingleViewController.h"


@interface MainViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UISegmentedControl *seg;
@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    self.seg  = [[UISegmentedControl alloc] initWithItems:@[@"训练计划", @"单次训练"]];
    self.seg.frame = CGRectMake(0, 0, 200, 30);
    self.navigationItem.titleView = self.seg;
    self.seg.tintColor = [UIColor grayColor];
    self.seg.selectedSegmentIndex = 0;
    NSDictionary *selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],NSForegroundColorAttributeName: [UIColor whiteColor]};
    [_seg setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],NSForegroundColorAttributeName: [UIColor grayColor]};
    [_seg setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    
    [self createScrollView];
    [self createChildrenVC];
    
}

#pragma mark - 创建ScrollView
- (void)createScrollView {
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.contentSize = CGSizeMake(SCREENWIDTH *2, 0);
    self.scrollView.delegate = self;
//    self.scrollView.backgroundColor = [UIColor yellowColor];
    
    [self.view addSubview:self.scrollView];
    
}

#pragma mark - 创建子视图控制器
- (void)createChildrenVC {
    
    VideoViewController *viewController = [[VideoViewController alloc] init];
    viewController.view.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
    [self addChildViewController:viewController];
    [self.scrollView addSubview:viewController.view];
    
    SingleViewController *singleVC = [[SingleViewController alloc] init];
    singleVC.view.frame = CGRectMake(SCREENWIDTH, 0, SCREENWIDTH, SCREENHEIGHT);
    [self addChildViewController:singleVC];
    [self.scrollView addSubview:singleVC.view];
}

#pragma mark - seg与ScrollView关联
- (void)segAction:(UISegmentedControl *)seg {
    
    CGFloat num = self.seg.selectedSegmentIndex;
    [self.scrollView setContentOffset:CGPointMake(SCREENWIDTH * num, 0) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    CGFloat num = self.scrollView.contentOffset.x / self.view.frame.size.width;
    self.seg.selectedSegmentIndex = num;

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
